package hos.location;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.LocationManager;

import java.io.IOException;
import java.util.Collections;
import java.util.List;


/**
 * <p>Title: LocationSourceManager </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/9/15 15:47
 */

public class LocationSourceManager implements LocationSourceListener {
    private static LocationSourceManager mLocationSourceManager;

    private LocationSourceManager() {
    }

    public static LocationSourceManager getInstance() {
        if (mLocationSourceManager == null) {
            synchronized (LocationSourceManager.class) {
                if (mLocationSourceManager == null) {
                    mLocationSourceManager = new LocationSourceManager();
                }
                return mLocationSourceManager;
            }
        }
        return mLocationSourceManager;
    }

    private LocationSource locationSource;

    /**
     * 只执行一次
     *
     * @param locationSource 实例化定位
     */
    public void init(LocationSource locationSource) {
        if (this.locationSource == null) {
            this.locationSource = locationSource;
        }
    }

    public LocationSource getLocationSource() {
        return locationSource;
    }

    public void reset(LocationSource locationSource) {
        this.locationSource = locationSource;
    }

    @Override
    public void setHasAddress(boolean hasAddress) {
        if (locationSource == null) {
            return;
        }
        locationSource.setHasAddress(hasAddress);
    }

    @Override
    public void openCache() {
        if (locationSource == null) {
            return;
        }
        locationSource.openCache();
    }

    @Override
    public void closeCache() {
        if (locationSource == null) {
            return;
        }
        locationSource.closeCache();
    }

    @Override
    public Context getContext() {
        if (locationSource == null) {
            return null;
        }
        return locationSource.getContext();
    }

    @Override
    public void clearLocationInfo() {
        if (locationSource == null) {
            return;
        }
        locationSource.clearLocationInfo();
    }

    @Override
    public boolean isLocationEnabled() {
        if (locationSource == null) {
            Context context = getContext();
            if (context == null) {
                return false;
            }
            return LocationSetting.isLocationEnabled((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
        }
        return locationSource.isLocationEnabled();
    }

    @Override
    public LocationSourceListener setLocationPurpose(LocationPurpose locationPurpose) {
        if (locationSource == null) {
            return null;
        }
        return locationSource.setLocationPurpose(locationPurpose);
    }

    @Override
    public void locationPurposeStart(LocationPurpose locationPurpose) {
        if (locationSource == null) {
            return;
        }
        locationSource.locationPurposeStart(locationPurpose);
    }

    @Override
    public void addLocationChangedListener(LocationChangedListener listener) {
        if (locationSource == null) {
            return;
        }
        locationSource.addLocationChangedListener(listener);
    }

    @Override
    public void removeLocationChangedListener(LocationChangedListener listener) {
        if (locationSource == null) {
            return;
        }
        locationSource.removeLocationChangedListener(listener);
    }

    @Override
    public void addStatusChangedListener(LocationStatusChangedListener listener) {
        if (locationSource == null) {
            return;
        }
        locationSource.addStatusChangedListener(listener);
    }

    @Override
    public void removeStatusChangedListener(LocationStatusChangedListener listener) {
        if (locationSource == null) {
            return;
        }
        locationSource.removeStatusChangedListener(listener);
    }

    @Override
    public void addHeadingChangedListener(LocationHeadingChangedListener listener) {
        if (locationSource == null) {
            return;
        }
        locationSource.addHeadingChangedListener(listener);
    }

    @Override
    public void removeHeadingChangedListener(LocationHeadingChangedListener listener) {
        if (locationSource == null) {
            return;
        }
        locationSource.removeHeadingChangedListener(listener);
    }

    @Override
    public Throwable getError() {
        if (locationSource == null) {
            return null;
        }
        return locationSource.getError();
    }

    @Override
    public void getOnceLocation(LocationOnceListener locationOnceListener) {
        if (locationSource == null) {
            return;
        }
        locationSource.getOnceLocation(locationOnceListener);
    }

    @Override
    public LocationData getLocation() {
        if (locationSource == null) {
            return null;
        }
        return locationSource.getLocation();
    }

    @Override
    public LocationData getLastKnownLocation() {
        if (locationSource == null) {
            return null;
        }
        return locationSource.getLastKnownLocation();
    }

    @Override
    public List<Address> getAddress() throws IOException {
        if (locationSource == null) {
            return null;
        }
        return locationSource.getAddress();
    }

    @Override
    public List<Address> getAddress(LocationData locationData) throws IOException {
        if (locationSource == null) {
            return null;
        }
        return locationSource.getAddress(locationData);
    }

    @Override
    public boolean isLatLngRepeated() {
        if (locationSource == null) {
            return false;
        }
        return locationSource.isLatLngRepeated();
    }

    @Override
    public void updateLocation(LocationData location) {
        if (locationSource == null) {
            return;
        }
        locationSource.updateLocation(location);
    }

    @Override
    public void notifyLocation() {
        if (locationSource == null) {
            return;
        }
        locationSource.notifyLocation();
    }

    @Override
    public void restartLocation() {
        if (locationSource == null) {
            return;
        }
        locationSource.restartLocation();
    }

    @Override
    public void startLocation() {
        if (locationSource == null) {
            return;
        }
        locationSource.startLocation();
    }

    @Override
    public void stopLocation() {
        if (locationSource == null) {
            return;
        }
        locationSource.stopLocation();
    }

    @Override
    public void closeLocation() {
        if (locationSource == null) {
            return;
        }
        locationSource.closeLocation();
    }

    @Override
    public void startForegroundLocation(Activity activity) {
        if (locationSource == null) {
            return;
        }
        locationSource.startForegroundLocation(activity);
    }

    @Override
    public void stopForegroundLocation() {
        if (locationSource == null) {
            return;
        }
        locationSource.stopForegroundLocation();
    }


}
